﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using BAL.EstRec.Common;

namespace BAL.EstRec.HQConsolidacion
{
    public class VMHqCon : INotifyPropertyChanged, IDlgLoaderDataContext
    {
        #region Fields
        private BgtVersion _bgtVersion;
        private string _status;
        public ConsolidacionCollection entries;
        public AppConfs cnfs;
        private ModelConsolidate mConsolidate;
        #endregion

        #region Properties
        public bool IsReadMode
        {
            get
            {
                bool ret = false;
                if (this._bgtVersion.isConsolidated)
                {
                    ret = true;
                }
                return ret;
            }
        }
        public long UnidVersion
        {
            get
            {
                return this._bgtVersion.unid;
            }
        }
        public string Status
        {
            get { return _status; }
            set
            {
                if (value != _status)
                {
                    this._status = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Status"));
                    }
                }
            }
        }
        #endregion Properties

        public VMHqCon(long _unidVersion,IConsolidacionHQUIRefresh uiRefresher)
        {
            try
            {
                this.mConsolidate = new ModelConsolidate(new AppConfs(), _unidVersion, this, uiRefresher);
                this.loadData(_unidVersion);
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void loadData(long unidVersion)
        {
            this._bgtVersion = BgtVersion.GetDataWithVersion(unidVersion.ToString());

            if (this._bgtVersion == null)
            {
                this._bgtVersion = new BgtVersion();
            }

            //Solo se intenta cargar la información si la versión ha sido consolidad
            this.mConsolidate.UnidVersion = unidVersion;

            if (this._bgtVersion.isConsolidated)
            {
                this.entries.Clear();
                this.entries.load(new ConsolidacionCollection(unidVersion));
                this.Status = "Versión Consolidada";
            }
            else
            {
                this.entries = new ConsolidacionCollection(0);
                this.Status = "";
            }
        }

        public void loadData()
        {
            if (this._bgtVersion != null && this._bgtVersion.unid != 0)
            {
                this.loadData(this._bgtVersion.unid);
            }
        }

        public void exportToExcel(IDlgConsolidateConfirmation confirmation,IFileChooser f)
        {
            if (this.entries.Count > 0)
            {
                string fullFilePath=f.getPath("Excel|*.xlsx");
                string tmp = "POS\tRECURSO\tCantidad Consolidada\n";

                if(!String.IsNullOrEmpty(fullFilePath))
                {
                    foreach (Consolidacion c in this.entries)
                    {
                       tmp+=c.Pos.idPos+"\t"+c.Recurso.idRecurso+"\t"+c.Confirmed.ToString()+"\n";
                    }
                    System.Windows.Clipboard.SetData(System.Windows.DataFormats.Text,tmp);

                    try
                    {
                        ExportExcel ee = new ExportExcel(fullFilePath, true, this.UnidVersion.ToString());
                        ee.ClipboardToExcel(false, fullFilePath, false);
                        
                    }
                    catch (Exception ex)
                    {
                        f.endProcess();
                        throw ex;
                    }
                }
                f.endProcess();
            }
        }

        public void consolidate(IDlgLoader dlg,IDlgConsolidateConfirmation confirmation)
        {
            if (!this._bgtVersion.isConsolidated)
            {
                if (this._bgtVersion.isPublished && this._bgtVersion.isSend)
                {
                    if (confirmation.getUserConfirmation("¿Está seguro de consolidar esta versión?\nSi consolida la versión ya no podrá ser editada."))
                    {
                        this.mConsolidate.consolidateStart();
                        dlg.show(this);
                    }
                }
                else
                {
                    confirmation.showConsolidateRefuse("No se puede consolidar esta versión por que no ha sido publicada o enviada.");
                }
            }
            else
            {
                confirmation.showConsolidateRefuse("Esta version ya está consolida.");
            }
            
        }

        public bool isVersionConsolidated()
        {
            return this._bgtVersion.isConsolidated;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #region IDlgLoaderDataContext
        private string _message;
        private bool _jobDone;
        private bool _manualClose;

        public string Message
        {
            get
            {
                return _message;
            }
            set
            {
                if (value != _message)
                {
                    this._message = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Message"));
                    }
                }
            }
        }

        public bool JobDone
        {
            get
            {
                return _jobDone;
            }
            set
            {
                if (value != _jobDone)
                {
                    this._jobDone = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("JobDone"));
                    }
                }
            }
        }

        public bool ManualClose
        {
            get
            {
                return _manualClose;
            }
            set
            {
                if (value != _manualClose)
                {
                    this._manualClose = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ManualClose"));
                    }
                }
            }
        }
        #endregion
    }// end class
}
